//
//  PxOilNetSession.m
//  OilCard
//
//  Created by Xu Yefeng on 14-5-18.
//  Copyright (c) 2014年 Free. All rights reserved.
//

#import "PxFormValidate.h"

PxFormValidate *sharedInstance;

@implementation PxFormValidate

+ (instancetype)sharedInstance
{
    if (sharedInstance == nil)
    {
        @synchronized(self)
        {
            if (sharedInstance == nil)
            {
                sharedInstance = [[self alloc] init];
            }
        }
    }
    return sharedInstance;
}

- (BOOL)checkNumber:(NSString *)string
{
    if (nil == string || string.length == 0)
        return YES;
    
    NSError *error             = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[0-9]" options:0 error:&error];
    NSUInteger numberOfMatches = [regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, string.length)];
    
    return numberOfMatches == string.length;
}

- (BOOL)checkPhone:(NSString *)string
{
    NSString *regex = @"^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\\d{8})$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isValid = [predicate evaluateWithObject:string];
    
    return isValid;
}

- (BOOL)checkEmail:(NSString *)string
{
    NSString *regex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isValid = [predicate evaluateWithObject:string];
    
    return isValid;
}

- (BOOL)checkYDH:(NSString *)string
{
    NSString *regex = @"^(K|k)\\d+(E|e)(D|d)$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isValid = [predicate evaluateWithObject:string];
    
    return isValid;
}

@end
